<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="姓名" prop="driverName" v-hasRole="['admin']">
        <el-input style="width: 130px;"
          v-model="queryParams.driverName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="userIdCard">
        <el-input style="width: 150px;"
          v-model="queryParams.userIdCard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="驾驶证号" prop="driverDrivingLicence" v-hasRole="['admin']">
        <el-input style="width: 150px;"
          v-model="queryParams.driverDrivingLicence"
          placeholder="请输入驾驶证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone" v-hasRole="['admin']">
        <el-input style="width: 130px;"
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="requirementsList" :max-height="tableHeight" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="姓名" align="center" prop="driverName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="身份证号" align="center" prop="userIdCard" />
      <!--<el-table-column label="驾驶证号" align="center" prop="driverDrivingLicence" />-->
      <el-table-column label="从业资格证号" align="center" prop="driverTransportationLicense" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员从业资格对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="980px">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">

        <el-col :span="12">
          <el-form-item label="姓名:" >{{form.driverName}}</el-form-item>
          <el-form-item label="身份证号:">{{form.userIdCard}}</el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="手机号:" >{{form.phone}}</el-form-item>
          <el-form-item label="从业人员驾驶证号:" >{{form.driverDrivingLicence}}</el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="驾驶证有效期始:">{{form.driverDrivingStartDate}}</el-form-item>
          <el-form-item label="驾驶证有效时长:" >{{form.driverDrivingEndDate}}</el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="从业资格类别:">{{form.driverTransportationType}}</el-form-item>
          <el-form-item label="驾驶证准驾车型:">{{form.driverDrivingVehicleType}}</el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="资格证初领日期:" v-if="form.driverTransportationInceptionDate != null">{{form.driverTransportationInceptionDate| dateString('YYYY-MM-DD')}}</el-form-item>
          <el-form-item label="资格证初领日期"  v-else></el-form-item>
          <el-form-item label="从业资格证号:">{{form.driverTransportationLicense}}</el-form-item>
        </el-col>

        <el-row>
        <el-col :span="12">
          <el-form-item label="资格证有效时间:" v-if="form.driverTransportationValidDate != null">{{form.driverTransportationValidDate| dateString('YYYY-MM-DD')}}</el-form-item>
          <el-form-item label="资格证有效时间"  v-else></el-form-item>
          <el-form-item></el-form-item>
        </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="驾驶证主页照片:" v-if="form.driverDrivingFirstPageUrl != null && form.driverDrivingFirstPageUrl != ''">
              <el-image :src="form.driverDrivingFirstPageUrl" style="width: 100px; height: 100px" :preview-src-list="srcList">
                <div slot="error" class="image-slot">
                  暂无图片
                </div>
              </el-image>
            </el-form-item>
            <el-form-item label="驾驶证主页照片" v-else>
              暂无图片
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="驾驶证副页照片:" v-if="form.driverDrivingSecondPageUrl == undefined || form.driverDrivingSecondPageUrl == ''">
              暂无图片
            </el-form-item>
            <el-form-item label="驾驶证副页照片:" v-else>
              <el-image :src="form.driverDrivingSecondPageUrl" style="width: 100px; height: 100px" :preview-src-list="srcList">
                <div slot="error" class="image-slot">
                  暂无图片
                </div>
              </el-image>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="从业资格证书照片:" v-if="form.driverTransportationPageUrl != null && form.driverTransportationPageUrl != ''">
            <el-image :src="form.driverTransportationPageUrl" style="width: 100px; height: 100px" :preview-src-list="srcList" >
              <div slot="error" class="image-slot">
                暂无图片
              </div>
            </el-image>
          </el-form-item>
          <el-form-item label="从业资格证书照片" v-else>
            暂无图片
          </el-form-item>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listRequirements, getRequirements } from "@/api/weight/requirements";


  export default {
    name: "Requirements",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 人员从业资格表格数据
        requirementsList: [],
        // 认证状态
        dict_isValid: [],
        // 认证类型
        dict_authenticationtype: [],
        // 弹出层标题
        title: "",

        srcList:[],
        // 是否显示弹出层
        open: false,
        tableHeight: window.innerHeight - 275,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          driverName: undefined,
          userIdCard: undefined,
          driverDrivingLicence: undefined,
          driverDrivingStartDate: undefined,
          driverDrivingEndDate: undefined,
          driverDrivingVehicleType: undefined,
          driverDrivingFirstPageUrl: undefined,
          driverDrivingSecondPageUrl: undefined,
          driverTransportationLicense: undefined,
          driverTransportationType: undefined,
          driverTransportationInceptionDate: undefined,
          driverTransportationValidDate: undefined,
          driverTransportationPageUrl: undefined,
          isValid: undefined,
          validReason: undefined,
          status: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          driverName: [
            { required: true, message: "人员姓名不能为空", trigger: "blur" }
          ],
          userIdCard: [
            { required: true, message: "人员身份证号不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
      // 认证状态
      this.getDicts("is_valid").then(response => {
        this.dict_isValid = response.data;
      });

      this.getDicts("authentication_type").then(response => {
        this.dict_authenticationtype = response.data;
      });


    },
    methods: {
      /** 查询人员从业资格列表 */
      getList() {
        this.loading = true;
        listRequirements(this.queryParams).then(response => {
          this.requirementsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          requirementId: undefined,
          driverName: undefined,
          userIdCard: undefined,
          driverDrivingLicence: undefined,
          driverDrivingStartDate: undefined,
          driverDrivingEndDate: undefined,
          driverDrivingVehicleType: undefined,
          driverDrivingFirstPageUrl: undefined,
          driverDrivingSecondPageUrl: undefined,
          driverTransportationLicense: undefined,
          driverTransportationType: undefined,
          driverTransportationInceptionDate: undefined,
          driverTransportationValidDate: undefined,
          driverTransportationPageUrl: undefined,
          isValid: undefined,
          validReason: undefined,
          status: "0",
          delFlag: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.requirementId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加人员从业资格";
      },
      /** 修改按钮操作 */
      handleView(row) {
        this.reset();
        const requirementId = row.requirementId || this.ids
        getRequirements(requirementId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "驾驶员详情";

          this.srcList.push(this.form.driverDrivingFirstPageUrl);
          this.srcList.push(this.form.driverDrivingSecondPageUrl);
          this.srcList.push(this.form.driverTransportationPageUrl);
        });
      },
      // 认证状态
      isValidFormat(row,column){
        return this.selectDictLabel(this.dict_isValid, row.isValid);
      },
      authenticationtypeFormat(row,column){
        return this.selectDictLabel(this.dict_authenticationtype, row.authenticationtype);
      }
    }
  };
</script>
